package com.koicarp.university.graduate.auth.configure;

import com.koicarp.university.graduate.auth.handle.PerAccessDeniedHandler;
import com.koicarp.university.graduate.auth.handle.UniversityAuthExceptionEntryPoint;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;

import javax.annotation.Resource;

/**
 * @auther liutao
 * @Date 2020-08-17 11:25:27
 */
@Configuration
@EnableResourceServer
public class UniversityResourceServerConfigure extends ResourceServerConfigurerAdapter {

    @Resource
    private PerAccessDeniedHandler perAccessDeniedHandle;

    @Resource
    private UniversityAuthExceptionEntryPoint authExceptionEntryPoint;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.authenticationEntryPoint(authExceptionEntryPoint)
                .accessDeniedHandler(perAccessDeniedHandle);
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .requestMatchers().antMatchers("/**")
                .and()
                .authorizeRequests()
                .antMatchers("/webjars/**","/swagger-ui.html/**","/swagger-resources/**","/v2/**","/csrf/**","/wx/**").permitAll()
                .antMatchers("/**").authenticated();
    }
}
